﻿Imports FairShop
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports System.Data.OleDb
Imports System.IO

<TestClass()> _
Public Class TestToolsDal

    Private testContextInstance As TestContext

    '''<summary>
    '''Gets or sets the test context which provides
    '''information about and functionality for the current test run.
    '''</summary>
    Public Property TestContext() As TestContext
        Get
            Return testContextInstance
        End Get
        Set(ByVal value As TestContext)
            testContextInstance = value
        End Set
    End Property

#Region "Additional test attributes"
    '
    ' You can use the following additional attributes as you write your tests:
    '
    ' Use ClassInitialize to run code before running the first test in the class
    <ClassInitialize()> _
    Public Shared Sub MyClassInitialize(ByVal testContext As TestContext)
        AppDomain.CurrentDomain.SetData("APPBASE", Environment.CurrentDirectory)
    End Sub
    '
    ' Use ClassCleanup to run code after all tests in a class have run
    ' <ClassCleanup()> Public Shared Sub MyClassCleanup()
    ' End Sub
    '
    ' Use TestInitialize to run code before running each test
    ' <TestInitialize()> Public Sub MyTestInitialize()
    ' End Sub
    '
    ' Use TestCleanup to run code after each test has run
    ' <TestCleanup()> Public Sub MyTestCleanup()
    ' End Sub
    '
#End Region

    <TestMethod()> _
    Public Sub TestImportArtikels()
        Dim strDatabankSource As String = GetTestImportFileNaam()
        Dim teImporteren As Integer = GetVoorraad(strDatabankSource, TestData.TEST_ID_ARTIKEL)
        Dim strDatabankTarget As String = strDatabankSource.Replace("FairShopTesting\TestData\FairShopSJC2010.mdb", "FairShop\bin\x86\Debug\App_Data\FairShopOefen.mdb")
        Dim initial As Integer = GetVoorraad(strDatabankTarget, TestData.TEST_ID_ARTIKEL)
        ToolsDal.ImportArtikels(strDatabankSource, strDatabankTarget, True, True)
        Dim actual As Integer = GetVoorraad(strDatabankTarget, TestData.TEST_ID_ARTIKEL)
        Assert.AreEqual(initial + teImporteren, actual)
    End Sub

    <TestMethod()> _
    Public Sub TestImportLevering()
        Dim strDatabankSource As String = GetTestImportFileNaam()
        Dim teImporteren As Integer = GetLeveringsaantal(strDatabankSource)
        Dim strDatabankTarget As String = strDatabankSource.Replace("FairShopTesting\TestData\FairShopSJC2010.mdb", "FairShop\bin\x86\Debug\App_Data\FairShopOefen.mdb")
        Dim initial As Integer = GetVoorraad(strDatabankTarget, TestData.TEST_ID_ARTIKEL_LEVERING)
        ToolsDal.ImportLevering(strDatabankSource, strDatabankTarget, TestData.TEST_ID_LEVERING, True, True)
        Dim actual As Integer = GetVoorraad(strDatabankTarget, TestData.TEST_ID_ARTIKEL_LEVERING)
        Assert.AreEqual(initial + teImporteren, actual)
    End Sub

    <TestMethod()> _
    Public Sub TestImportVerkoop()
        Dim strDatabankSource As String = GetTestImportFileNaam()
        Dim teImporteren As Integer = GetVerkoopsaantal(strDatabankSource)
        Dim strDatabankTarget As String = strDatabankSource.Replace("FairShopTesting\TestData\FairShopSJC2010.mdb", "FairShop\bin\x86\Debug\App_Data\FairShopOefen.mdb")
        Dim initial As Integer = GetVoorraad(strDatabankTarget, TestData.TEST_ID_ARTIKEL_VERKOOP)
        ToolsDal.ImportVerkoop(strDatabankSource, strDatabankTarget, TestData.TEST_ID_VERKOOP, True, True)
        Dim actual As Integer = GetVoorraad(strDatabankTarget, TestData.TEST_ID_ARTIKEL_VERKOOP)
        Assert.AreEqual(initial - teImporteren, actual)
    End Sub

#Region "Helpermethods"

    Public Function GetVoorraad(ByVal strDatabank As String, ByVal idArtikel As Integer) As Integer
        Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDatabank
        Using conn As New OleDbConnection(strConn)
            conn.Open()
            Dim strSQL As String = "SELECT Voorraad FROM Artikels WHERE Id=" & idArtikel.ToString
            Dim cm As New OleDbCommand(strSQL, conn)
            Return Convert.ToInt32(cm.ExecuteScalar())
        End Using
    End Function

    Public Function GetLeveringsaantal(ByVal strDatabank As String) As Integer
        Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDatabank
        Using conn As New OleDbConnection(strConn)
            conn.Open()
            Dim strSQL As String = "SELECT Aantal FROM LeveringDetails WHERE Id=" & TestData.TEST_ID_LEVERING_DETAILS
            Dim cm As New OleDbCommand(strSQL, conn)
            Return Convert.ToInt32(cm.ExecuteScalar())
        End Using
    End Function

    Public Function GetVerkoopsaantal(ByVal strDatabank As String) As Integer
        Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDatabank
        Using conn As New OleDbConnection(strConn)
            conn.Open()
            Dim strSQL As String = "SELECT Aantal FROM VerkoopsDetails WHERE Id=" & TestData.TEST_ID_VERKOOP_DETAILS
            Dim cm As New OleDbCommand(strSQL, conn)
            Return Convert.ToInt32(cm.ExecuteScalar())
        End Using
    End Function

    Private Function GetTestImportFileNaam() As String
        Dim diTestDeployFolder As New DirectoryInfo(TestContext.TestDir)
        Dim diSolutionRoot As DirectoryInfo = diTestDeployFolder.Parent.Parent
        Dim diTestFolder As DirectoryInfo = diSolutionRoot.GetDirectories("FairShopTesting")(0)
        Dim diTestDataFolder As DirectoryInfo = diTestFolder.GetDirectories("TestData")(0)
        Dim testImportFileNaam As String = Path.Combine(diTestDataFolder.FullName, "FairShopSJC2010.mdb")
        Return testImportFileNaam
    End Function

#End Region

End Class
